package com.zjlanyun.eplus.view.report.adapter;

import android.content.Context;
import android.graphics.Color;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Toast;

import com.zjlanyun.eplus.R;
import com.zjlanyun.eplus.view.report.custom.innerListView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import lecho.lib.hellocharts.gesture.ZoomType;
import lecho.lib.hellocharts.listener.ColumnChartOnValueSelectListener;
import lecho.lib.hellocharts.listener.PieChartOnValueSelectListener;
import lecho.lib.hellocharts.model.Axis;
import lecho.lib.hellocharts.model.AxisValue;
import lecho.lib.hellocharts.model.Column;
import lecho.lib.hellocharts.model.ColumnChartData;
import lecho.lib.hellocharts.model.PieChartData;
import lecho.lib.hellocharts.model.SliceValue;
import lecho.lib.hellocharts.model.SubcolumnValue;
import lecho.lib.hellocharts.view.ColumnChartView;
import lecho.lib.hellocharts.view.PieChartView;

import static com.zjlanyun.eplus.view.report.UtilConstants.KEY_ADAPTER_LISTNAME;
import static com.zjlanyun.eplus.view.report.UtilConstants.KEY_ADAPTER_TABLENAME;
import static com.zjlanyun.eplus.view.report.UtilConstants.KEY_ADAPTER_VALUE;

/**
 * 作者：韩笑 on 2017/6/16 0016 09:34
 * <p>
 * 作用：列表布局ListView适配器
 */


public class MyListViewAdapter extends BaseAdapter {
    private static final String TAG = "MyListViewAdapter";

    //统计图属性设置
    private boolean hasLabels = true;//是否在薄片上显示label
    private boolean hasLabelsOutside = true;//是否在薄片外显示label
    private boolean hasCenterCircle = true;//是否中间掏空一个圈
    private boolean hasLabelForSelected = false;
    private int PIE = 1; //饼状图
    private int HISTOGRM = 2;//柱状图
    private float CHART_SIZE = 0.7f ;//饼状图大小
    private float RINGSIZE = 0.3f;//饼状图环形大小级别

    private Context mContext;
    private List<List<showBean>> showList;
    private List<showBean> list;

    //统计图数据设置
    private ColumnChartData data;
    private List<AxisValue> axisValues;
    private List<Column> columns;
    //X轴
    public String[] months;
    //柱图显示的颜色
    public int [] color ={Color.parseColor("#FF4444"), Color.parseColor("#E3D713"), Color.parseColor("#33B5E5"), Color.parseColor("#31E317")
    ,Color.parseColor("#FF4444"), Color.parseColor("#E3D713"), Color.parseColor("#33B5E5"), Color.parseColor("#31E317")};

    /**
     * 构造方法
     * @param mContext
     * @param showList
     */
    public MyListViewAdapter(Context mContext, List<List<showBean>> showList) {
        this.mContext = mContext;
        this.showList = showList;
    }

    @Override
    public int getCount() {
        return  showList.size();
    }

    @Override
    public Object getItem(int position) {
        return showList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.listview_item, null);
            holder.item_list = (innerListView) convertView.findViewById(R.id.item_list);
            holder.chart = (ColumnChartView) convertView.findViewById(R.id.chart);
            holder.chart1 = (PieChartView) convertView.findViewById(R.id.chart1);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        if (Integer.parseInt(showList.get(position).get(1).getFCartogramTypr())  == PIE){
            //Log.d(TAG,showList.get(position).get(1).getFCartogramTypr()+"饼图");
                holder.chart.setVisibility(View.GONE);
                holder.chart1.setVisibility(View.VISIBLE);
                setChartDate(holder.chart1,position);
        }else{
            //Log.d(TAG,showList.get(position).get(1).getFCartogramTypr()+"柱状图");
               holder.chart1.setVisibility(View.GONE);
               holder.chart.setVisibility(View.VISIBLE);
               setChartDate(holder.chart,position);
        }


        List<Map> itemList =new ArrayList<>();
        for (int i = 0 ;i < showList.get(position).size();i++){
            Map<String,String> map = new HashMap<>();
            map.put(KEY_ADAPTER_LISTNAME,showList.get(position).get(i).getFxValue());
            map.put(KEY_ADAPTER_TABLENAME,showList.get(position).get(i).getFCorresPondingTable());
            map.put(KEY_ADAPTER_VALUE,showList.get(position).get(i).getFyValue());
            itemList.add(map);
        }

        ListItemAdapter adapter = new ListItemAdapter(itemList, mContext);
        holder.item_list.setAdapter(adapter);
        return convertView;
    }

    class ViewHolder {
        innerListView item_list;
        ColumnChartView chart;
        PieChartView chart1;
    }


    /**
     * 加入饼状图
     * @param chart 饼状图
     * @param postion 位置
     */
    public void setChartDate(PieChartView chart, int postion) {
        PieChartData data;
        chart.setOnValueTouchListener(new ValueTouchListener1());
        chart.setCircleFillRatio(CHART_SIZE);//设置饼图大小

        List<SliceValue> values = new ArrayList<SliceValue>();
        list = showList.get(postion);
        for (int i = 0; i < list.size(); i++) {
            int value = Integer.parseInt(list.get(i).getFyValue());
            SliceValue sliceValue = new SliceValue(value, color[i]);
            sliceValue.setLabel(list.get(i).getFxValue() + ":" + (int) sliceValue.getValue() + "元");//设置label
            values.add(sliceValue);

        }


        data = new PieChartData(values);
        data.setCenterCircleScale(RINGSIZE);//设置环形的大小级别
        data.setHasLabelsOutside(false);
        data.setHasLabels(hasLabels);
        data.setHasLabelsOnlyForSelected(hasLabelForSelected);
        data.setHasLabelsOutside(hasLabelsOutside);//占的百分比是否显示在饼图外面
        data.setHasCenterCircle(hasCenterCircle);
        chart.setPieChartData(data);
    }

    /**
     * 饼状图监听
     */
    private class ValueTouchListener1 implements PieChartOnValueSelectListener {
        @Override
        public void onValueSelected(int arcIndex, SliceValue value) {
           // Toast.makeText(mContext,.get(arcIndex).getxValue()+":￥" + value.getValue() + "元", Toast.LENGTH_SHORT).show();

        }

        @Override
        public void onValueDeselected() {
            // TODO Auto-generated method stub
        }
    }

    /**
     * 加入柱形图
     * @param chart 柱形图
     * @param position 位置
     */
    public void setChartDate(ColumnChartView chart, int position) {
        chart.setOnValueTouchListener(new ValueTouchListener());
        columns = new ArrayList<Column>();
        axisValues = new ArrayList<AxisValue>();

        List<showBean> tt = new ArrayList<>();
        tt = showList.get(position);

        months = new String[tt.size()];
        for (int i = 0; i < tt.size(); i++) {
            months[i] = tt.get(i).getFxValue();
        }
        for (int i = 0; i < months.length; i++) {
            List<SubcolumnValue> values = new ArrayList<SubcolumnValue>();
            values.add(new SubcolumnValue(Integer.parseInt(tt.get(i).getFyValue()),color[i]));
            Column column = new Column(values);
            axisValues.add(new AxisValue(i).setLabel(months[i]));
            columns.add(column.setHasLabelsOnlyForSelected(true));
            column.setHasLabels(true);// 是否显示节点数据
        }


        data = new ColumnChartData(columns);
        data.setAxisXBottom(new Axis(axisValues).setHasLines(true));
        chart.setValueSelectionEnabled(true);
        chart.setZoomType(ZoomType.HORIZONTAL);
        Axis axisY = new Axis().setHasLines(true);
        axisY.setValues(null);
        axisY.setHasLines(false);// 是否显示Y轴网格线
        data.setAxisYLeft(axisY);
        chart.setColumnChartData(data);
    }

    /**
     * 柱形图监听
     */
    private class ValueTouchListener implements ColumnChartOnValueSelectListener {
        @Override
        public void onValueSelected(int columnIndex, int subcolumnIndex, SubcolumnValue value) {
//            Toast.makeText(mContext, months[columnIndex] + ":  ￥" + value.getValue() + "元", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onValueDeselected() {
            // TODO Auto-generated method stub
        }
    }

    /**
     * 添加列表项
     */
    public void addItem(List<showBean> bean) {
        showList.add(bean);
    }
    /**
     * 重新加载
     */
    public void refreshData(List<List<showBean>> refreshList){
        this.showList = refreshList;
    }

}
